<template>
        <el-card shadow="never" style="max-height: 70vh; overflow: auto">
            <el-form ref="form" :model="form" :rules="rules" label-width="90px" :disabled="disabled">
                <el-form-item label="id:" prop="id" v-show="false">
                    <el-input v-model="form.id" disabled />
                </el-form-item>
                <el-form-item label="所属用户:" prop="user_id">
                    <SelectPage v-model="form.user_id" :url="'/admin/user/selectPage'" placeholder="请选择所属用户，不选为系统"
                                show_field="nickname" show_id="uid" query_field="uid" :multiple="false" />
                </el-form-item>
                <el-form-item label="策略标题:" prop="strategy_title">
                    <el-input v-model="form.strategy_title" placeholder="请输入策略标题" clearable />
                </el-form-item>
                <el-form-item label="策略类型:" prop="strategy_type">
                    <el-select v-model="form.strategy_type" placeholder="请选择策略类型" clearable>
                        <el-option v-for="(item, index) in strategy_type" :key="index" :label="item.label" :value="item.value" />
                    </el-select>
                </el-form-item>
                <el-form-item label="消息类型:" prop="msg_type">
                    <el-radio-group v-model="form.msg_type" @change="onChangeMsgType">
                        <el-radio v-for="(item, index) in msg_type" :key="index" :label="item.value">{{ item.label }}</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="消息内容:" prop="msg_content" v-if="form.msg_type === 'word'">
                    <el-input v-model="form.msg_content" placeholder="请输入消息内容" clearable />
                </el-form-item>
                <el-form-item label="消息内容:" prop="msg_content" v-if="form.msg_type === 'image'">
                    <ImageUpload v-model="form.msg_content" :fileType="['png', 'jpg', 'jpeg', 'gif']" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm">确认 </el-button>
                    <el-button type="warning" @click="cancel">取消 </el-button>
                </el-form-item>
            </el-form>
        </el-card>
</template>

<script>
import { regionData } from 'element-china-area-data'
export default {
    name: 'EditForm',
    props: {
        findFormData: {
            type: Object,
            default: {}
        },
        editFormParams: {
            type: Object,
            default: {}
        },
        // 表单是否禁用
        disabled: {
            type: Boolean,
            default: false,
        },
    },
    data() {
        return {
            // 编辑表单验证规则
            rules: {
                // user_id: [{ required: true, message: "请输入所属用户", trigger: "blur" }],
                strategy_title: [{ required: true, message: "请输入策略标题", trigger: "blur" }],
                strategy_type: [{ required: true, message: "请选择策略类型", trigger: "blur" }],
                msg_type: [{ required: true, message: "请选择消息类型", trigger: "blur" }],
                msg_content: [{ required: true, message: "消息内容为空", trigger: "blur" }],
            },
            form: {
                msg_type: 'word'
            },
            // 区域选择配置
            areaOptions: regionData,
            strategy_type:[
               { label: "默认", value: 1 },
               { label: "欢迎语", value: 2 },
            ],
            msg_type:[
               { label: "文本", value: 'word' },
               { label: "图片", value: 'image' },
            ],
        }
    },
    watch: {
        findFormData: {
           handler(nval) {
                if (JSON.stringify(nval) != '{}') {
                    this.$nextTick(() => {
                        nval.user_id = nval.user_id ? nval.user_id : ''
                        this.form = nval
                    })
                }
            },
            immediate: true,
            deep: false,
        },
    },
    methods: {
        // 取消按钮
        cancel() {
            this.$emit('editFormCancel')
        },
        submitForm() {
            this.$refs['form'].validate((valid, obj) => {
                if (valid) {
                    this.$emit('submitForm', this.form)
                }
            })
        },
        // 表单重置
        reset() {
            this.resetForm('form')
        },
        onChangeMsgType() {
            this.form.msg_content = ""
        }
    }
}
</script>
